#include "string.h"
namespace wx
{
	string::string(const char* str)
		: _size(strlen(str))
	{
		_str = new char[_size + 1];
		strcpy(_str, str);
		_capacity = _size;
	}

	char* string::c_str()
	{
		return _str;
	}

	void string::reserve(size_t n)
	{
		char* tmp = new char[n];
		strcpy(tmp, _str);
		delete[] _str;
		_str = tmp;
	}

	void string::push_back(char ch)
	{
		if (_size + 1 > _capacity)
		{
			reserve(_capacity == 0 ? 4 : _capacity * 2);
		}
		_str[_size] = ch;
		_size++;
		_str[_size] = '\0';
	}

	void string::append(const char* str)
	{
		size_t len = strlen(str);
		if (len + _size > _capacity)
		{
			int newCapacity = _capacity * 2;
			if (newCapacity < len + _size)
			{
				newCapacity = len + _size;
			}
			reserve(newCapacity);
		}
		strcpy(_str + _size, str);
		_size += len;
	}

	string::~string()
	{
		delete[] _str;
		_str = nullptr;
		_size = _capacity = 0;
	}
}

void test1()
{
	wx::string s1("12345");
	//s1.push_back('a');
	//s1.push_back('b');
	//s1.push_back('c');
	s1.append("zcv");

	cout << s1.c_str();
}
